/*
 * Copyright (c)e 2024-2024.Powered by neflibata
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.liuzian.marketserver.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liuzian.marketserver.dto.R;
import com.liuzian.marketserver.entity.Image;
import com.liuzian.marketserver.mapper.ImageMapper;
import com.liuzian.marketserver.service.ImageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Objects;
import java.util.UUID;

@Slf4j
@Service
public class ImageServiceImpl extends ServiceImpl<ImageMapper, Image> implements ImageService {

    @Value("${file.uploadFolder}")
    private String uploadFolder;

    @Override
    public R<List<Image>> queryRecommendImages() {
        LambdaQueryWrapper<Image> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Image::getLabel, 4);
        List<Image> imageServices = super.list(wrapper);
        return R.success(imageServices);
    }

    @Override
    public R<String> ImgStr(MultipartFile file, Long id, int label) throws IOException {
        if (file.equals("")) {
            return R.error("文件为空");
        }
        Image image = new Image();
        String imgFormat = Objects.requireNonNull(file.getOriginalFilename()).substring(file.getOriginalFilename().lastIndexOf("."));
        String nextPath;
        switch (label) {
            case 1:
                nextPath = "Commodity";
                image.setGid(id);
                break;
            case 2:
                nextPath = "More";
                image.setGid(id);
                break;
            case 3:
                nextPath = "User";
                image.setUid(id);
                break;
            default:
                nextPath = "Recommend";
                break;
        }
        image.setLabel(label);
        //判断文件是否存在
        File f = new File(uploadFolder + nextPath);
        try {
            if (!f.exists()) {
                f.mkdirs();
            }
        } catch (Exception e) {
            return R.error(e.getMessage());
        }
        //给图片重新随机生成名字
        String name = UUID.randomUUID() + imgFormat;
        //保存图片
        file.transferTo(new File(uploadFolder + nextPath + "/" + name));
        //拼接要保存在数据中的图片地址
        String url = nextPath + "/" + name;
        //放入对应的字段中
        image.setUrl(url);
        super.save(image);
        return R.success("图片上传成功");
    }

    @Override
    public Image getImageByUid(Long uid) {
        LambdaQueryWrapper<Image> imageWrapper = new LambdaQueryWrapper<>();
        imageWrapper.eq(Image::getUid, uid);
        imageWrapper.eq(Image::getLabel, 3);
        return super.getOne(imageWrapper);
    }

    @Override
    public Image getImageByGid(Long gid) {
        LambdaQueryWrapper<Image> imageLambdaQueryWrapper = new LambdaQueryWrapper<>();
        imageLambdaQueryWrapper.eq(Image::getGid, gid);
        imageLambdaQueryWrapper.eq(Image::getLabel, 1);
        return super.getOne(imageLambdaQueryWrapper);
    }
}
